package utils

import (

	"fmt"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/driver/mysql"
	"github.com/spf13/viper"
	"time"
	"os"
	"log"

)

func InitConfig() {
	viper.SetConfigName("app")
	viper.AddConfigPath("config")
	err := viper.ReadInConfig()
	if err!=nil {
		fmt.Println(err)
	}
	fmt.Println("Config app: ",viper.Get("app"))
	fmt.Println("Config mysql: ",viper.Get("mysql"))

}

var DB *gorm.DB

// InitMysql 初始化 MySQL 数据库连接
func InitMysql()  {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags),
		logger.Config{
			SlowThreshold: time.Second,
			LogLevel: logger.Info,
			Colorful: true,
		},
	)

    // 直接给全局变量 DB 赋值
    DB, _= gorm.Open(mysql.Open(viper.GetString("mysql.dns")), &gorm.Config{Logger: newLogger})

}